//
//  TransferManagerController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 2017/4/26.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "TransferManagerController.h"
#import "TextFieldCell.h"
#import "DefaultButton.h"

@interface TransferManagerController ()<UITextFieldDelegate>

@property(nonatomic,strong)MBProgressHUD *progressHUD;
@property(nonatomic,strong)UITextField *userTextField;
@property(nonatomic,strong)UITextField *moneyTextField;
@property(nonatomic,strong)UITextField *payTextField;
@property (nonatomic,strong)NSDictionary *userInfoDict;
@property(nonatomic,strong)UIButton *btn;

@end

@implementation TransferManagerController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.title = @"转账管理";
    self.tableView.rowHeight = 50;
    self.tableView.backgroundColor = BACKGROUND_COLOR;
    self.tableView.scrollEnabled = false;
    [self.tableView registerNib:[UINib nibWithNibName:@"TextFieldCell" bundle:nil] forCellReuseIdentifier:@"TextFieldCell"];
    
    [self loadUserInfoNumberData];
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, -25, 0, 0)];
        
    }
}

- (void)loadUserInfoNumberData {
    
    NSDictionary *params = @{
                             @"username":USERNAME
                             };
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"api/User/info" completeBlock:^(id object) {
        self.userInfoDict = object;
        [self.tableView reloadData];
    }];
}

- (void)transferDidClick
{
//    NSLog(@"%@",self.userTextField.text);
//    NSLog(@"%@",self.moneyTextField.text);
//    NSLog(@"%@",self.payTextField.text);
    
    [self registerAllTextField];
    
    if (!self.userTextField.text.length || !self.payTextField.text.length || !self.moneyTextField.text.length) {
        self.progressHUD = [MBProgressHUD showMessage:@"请输入完整信息" toView:self.view dimBackground:false AndCompleteBlock:nil];
        [self.view addSubview:self.progressHUD];
        return;
    }
    
    //请求过程中禁止点击
    self.btn.enabled = false;
    [self.btn setBackgroundColor:[UIColor lightGrayColor]];
    
    NSDictionary *params = @{@"username":USERNAME,
                             @"account_username":self.userTextField.text,
                             @"money":self.moneyTextField.text,
                             @"password":self.payTextField.text
                             };
    NSLog(@"%@",params);
    @weakify(self);
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/user/forward" completeBlock:^(id object) {
        NSLog(@"%@",object);
        
        @strongify(self);
        if (JudgeBackDataCode) {
            
            self.progressHUD = [MBProgressHUD showMessage:@"转账成功" toView:self.view dimBackground:false AndCompleteBlock:^(id object){
                [self.navigationController popToRootViewControllerAnimated:true];
            }];
            [self.view addSubview:self.progressHUD];
        }else{
            
            NSDictionary *dict = [object objectForKey:@"message"];
            self.progressHUD = [MBProgressHUD showMessage:[NSString stringWithFormat:@"%@",dict[@"msg"]] toView:self.view dimBackground:NO AndCompleteBlock:nil];
            [self.view addSubview:self.progressHUD];
            
        }
        

    }];

    
}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else{
        return 3;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSDictionary *dataDict = self.userInfoDict[@"data"];
    
    TextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TextFieldCell" forIndexPath:indexPath];
    cell.TextField.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.TextField.keyboardType = UIKeyboardTypeDecimalPad;
    cell.TextField.tag = indexPath.row;
    
    if (indexPath.section == 0) {
        
        cell.TextLabel.font = BOLDFONT(15);
        cell.TextLabel.text = @"账户余额";
        cell.TextField.text = dataDict[@"yue"];
        cell.TextField.textColor = DEFAULT_COLOR;
        cell.TextField.font =  BOLDFONT(15);
        cell.TextField.enabled = false;
    }else{
        
        if (indexPath.row == 0) {
            cell.TextLabel.text = @"商家号";
            cell.TextField.placeholder = @"请填入商家号或者推广员号";
            self.userTextField = cell.TextField;
            [self.userTextField becomeFirstResponder];
            
        }else if (indexPath.row == 1){
            cell.TextLabel.text = @"金 额";
            cell.TextField.placeholder = @"请输入金额";
            self.moneyTextField = cell.TextField;
        }else{
            cell.TextLabel.text = @"支付密码";
            cell.TextField.placeholder = @"请输入支付密码";
            cell.TextField.secureTextEntry = true;
            self.payTextField = cell.TextField;
            cell.TextField.keyboardType = UIKeyboardTypeDefault;
        }

    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 8;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (section == 0) {
        return 0.01;
    }else{
        return HEIGHT/6;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return nil;
    }else{
        return [self leaveView];
    }
}

- (UIView *)leaveView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,WIDTH, HEIGHT/6)];
    DefaultButton *button = [[DefaultButton alloc] init];
    self.btn = button;
    [button setTitle:@"确认提交" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(transferDidClick) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view.mas_centerX);
        make.centerY.equalTo(view.mas_centerY);
        make.height.mas_equalTo(view.Height/3);
        make.width.mas_equalTo(WIDTH-30);
    }];
    return view;
}


- (void)registerAllTextField {
    [self.userTextField resignFirstResponder];
    [self.moneyTextField resignFirstResponder];
    [self.payTextField resignFirstResponder];
    
}

@end
